package com.order.manager.controller.Shop;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.order.manager.Model.OrderInfo;
import com.order.manager.Model.ShopInfo;
import com.order.manager.Model.UserInfo;
import com.order.manager.Model.other.RestBean;
import com.order.manager.Model.other.ShopShow;
import com.order.manager.service.impl.OrderInfoServiceImpl;
import com.order.manager.service.impl.ShopInfoServiceImpl;
import com.order.manager.service.impl.UserInfoServiceImpl;
import com.order.manager.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/shop")
public class ShopController {

    @Resource
    ShopInfoServiceImpl shopInfoService;

    @Resource
    UserInfoServiceImpl userInfoService;

    @Resource
    JwtUtil jwtUtil;

    @Resource
    OrderInfoServiceImpl orderInfoService;

    /**
     * 在创建了商户之后进行创店
     *
     * @return
     */
    @RequestMapping("/insert")
    public RestBean applyShop(HttpServletRequest request) {
        String userId = jwtUtil.getJwtUsername(request);
        UserInfo userInfo = userInfoService.getById(userId);
        if (userInfo == null || userInfo.getState() != 1) {
            return RestBean.failure(403, "用户异常");
        }
        ShopInfo shopInfo = new ShopInfo();
        shopInfo.setShopName(userInfo.getName() + "的店铺");
        shopInfo.setShopId(UUID.randomUUID().toString());
        shopInfo.setShopState(0);
        shopInfo.setInputTime(new Date());
        shopInfo.setShopMasterId(userInfo.getUserId());
        shopInfo.setUpdateTime(new Date());
        return shopInfoService.save(shopInfo) ? RestBean.success(shopInfo) : RestBean.failure(500, "server error");
    }

    @RequestMapping("/update")
    public RestBean update(@RequestBody ShopInfo shopInfo) {
        shopInfo.setUpdateTime(new Date());
        return shopInfoService.updateById(shopInfo) ? RestBean.success(shopInfo) : RestBean.failure(500, "server error");
    }

    @RequestMapping("/select/{page}/{item}")
    public RestBean queryShop(@PathVariable int page, @PathVariable int item, String shopName) {
        return RestBean.success(shopInfoService.queryByShopName(page, item, shopName));
    }

    @RequestMapping("/select/collect/{page}/{item}")
    public RestBean queryByUserCollect(@PathVariable int page, @PathVariable int item, String shopName, HttpServletRequest request) {
        String uuid = jwtUtil.getJwtUsername(request);
        return RestBean.success(shopInfoService.queryByShopAndUserCollect(page, item, shopName, uuid));
    }


    @RequestMapping("/update/state/{shopId}/{state}")
    public RestBean updateState(@PathVariable String shopId, @PathVariable int state) {
        ShopInfo shopInfo = shopInfoService.getById(shopId);
        if (shopInfo == null) {
            return RestBean.failure(403, "该商店不存在");
        }
        if (state == 1 || state == 2 || state == 3 || state == -1 || state == 0) {
            shopInfo.setShopState(state);
            return shopInfoService.updateById(shopInfo) ? RestBean.success(shopId) : RestBean.failure(500, "server error");
        }
        return RestBean.failure(403, "state error");
    }

    @RequestMapping("/size")
    public RestBean shopSize() {
        return RestBean.success(shopInfoService.list(new QueryWrapper<ShopInfo>().eq("shop_state", 3)).size());
    }

    @RequestMapping("/info")
    public RestBean getInfo(HttpServletRequest request) {
        String uuid = jwtUtil.getJwtUsername(request);
        ShopInfo shopInfo = shopInfoService.getOne(new QueryWrapper<ShopInfo>().eq("shop_master_id", uuid));
        return shopInfo != null ? RestBean.success(shopInfo) : RestBean.failure(403, "没有开设店铺");
    }

    @RequestMapping("/get/{id}")
    public RestBean selectOne(@PathVariable String id) {
        ShopInfo shopInfo = shopInfoService.getById(id);
        return shopInfo == null ? RestBean.failure(403, "没有该记录") : RestBean.success(shopInfo);
    }

    @RequestMapping("/show")
    public RestBean show(HttpServletRequest request) {
        String uuid = jwtUtil.getJwtUsername(request);
        ShopInfo shopMasterId = shopInfoService.getOne(new QueryWrapper<ShopInfo>().eq("shop_master_id", uuid));
        if (shopMasterId == null) return RestBean.failure(403, "信息异常");
        ShopShow show = new ShopShow();
        List<OrderInfo> orderInfos = orderInfoService.list(new QueryWrapper<OrderInfo>().eq("shop_id", shopMasterId.getShopId()));
        BigDecimal totalMoney = BigDecimal.valueOf(0);
        int totalOrder = 0;
        for (OrderInfo o : orderInfos) {
            totalMoney = totalMoney.add(o.getMoney());
            totalOrder++;
        }
        show.setMoneyTotal(Double.valueOf(totalMoney.toString()));
        show.setOrderCount(totalOrder);
        List<OrderInfo> orderInfosToTotal = orderInfoService.list(new QueryWrapper<OrderInfo>().eq("shop_id", shopMasterId.getShopId()).apply("DATE(input_time) = DATE(NOW())"));
        BigDecimal todayTotalMoney = BigDecimal.valueOf(0);
        int todayTotalOrder = 0;

        for (OrderInfo o : orderInfosToTotal) {
            todayTotalMoney = todayTotalMoney.add(o.getMoney());
            todayTotalOrder++;
        }
        show.setTodayOrderCount(todayTotalOrder);
        show.setTodayTotalMoney(Double.valueOf(todayTotalMoney.toString()));
        return RestBean.success(show);
    }

    @RequestMapping("/query/school/{page}/{item}")
    public RestBean queryBySchool(@PathVariable int page,@PathVariable int item){
        return RestBean.success(shopInfoService.queryBySchool(page,item));
    }

}
